package com.dingding.manager.modules.order.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.dingding.manager.common.beans.order.SpecialOrderComment;
import com.dingding.manager.common.beans.order.UserOrder;
import com.dingding.manager.common.beans.order.UserSpecialOrder;
import com.dingding.manager.common.beans.order.UserSpecialOrdert;
import com.dingding.manager.common.beans.order.UserTaxiOrdert;
import com.dingding.manager.common.dao.Db;
import com.dingding.manager.common.dao.Page;
import com.dingding.manager.core.util.SortUtil;
import com.dingding.manager.modules.order.dao.IOrderDao;
import com.dingding.manager.modules.order.dto.ResOrderListDTO;
import com.dingding.manager.modules.order.enums.OrderStatus;
import com.dingding.manager.modules.order.sql.OrderSql;
import com.dingding.manager.modules.system.dao.impl.OrganizationDaoImpl;

/**
 * 
 * @ClassName: OrderDaoImpl
 * @Description: 司机来源dao 
 * @author hufan
 * @date 2016年12月7日 下午4:41:04
 *
 */
@Component("orderDao")
public class OrderDaoImpl implements IOrderDao {

	private static Logger logger = Logger.getLogger(OrganizationDaoImpl.class);

	/**
	 * 
	 * @Title: getResOrderDTOListByPage 
	 * @Description: 根据条件分页查询数据
	 * @param driverIds 司机ids
	 * @param orderStatus 订单状态 
	 * @param startTime 起始日期
	 * @param endDate 结束日期
	 * @param start 起始下标
	 * @param length 每页展示size
	 * @return  参数说明 
	 * @return Page<ResOrderDTO>    返回类型 
	 * @throws
	 */
	@Override
	public Page<ResOrderListDTO> getResOrderDTOListByPage(List<String> driverIds, OrderStatus orderStatus, String startTime,
			String endDate, int start, int length,String address) {

		List<ResOrderListDTO> resOrderDTOs = new ArrayList<ResOrderListDTO>();

		// 查询符合条件的出租车已完成的订单信息
		List<UserOrder> userOrders = Db.querySqlList(UserOrder.class,
				OrderSql.getUserOrderQuerySql(driverIds, orderStatus, startTime, endDate,address), driverIds.toArray());

		// 查询符合条件的专车已完成的订单
		List<UserSpecialOrder> userSpecialOrders = Db.querySqlList(UserSpecialOrder.class,
				OrderSql.getSpecialOrderQuerySql(driverIds, orderStatus, startTime, endDate,address), driverIds.toArray());

		// 如果查询状态不是已完成，则需要去两张临时表查询
		if (orderStatus != OrderStatus.COMPLETED) {
			// 查询符合条件的出租车未完成的订单
			List<UserTaxiOrdert> userTaxiOrderts = Db.querySqlList(UserTaxiOrdert.class,
					OrderSql.getTaxiOrdertQuerySql(driverIds, orderStatus, startTime, endDate,address), driverIds.toArray());
			// 查询符合条件的专车未完成的订单
			List<UserSpecialOrdert> userSpecialOrderts = Db.querySqlList(UserSpecialOrdert.class,
					OrderSql.getSpecialOrdertQuerySql(driverIds, orderStatus, startTime, endDate,address), driverIds.toArray());

			if (CollectionUtils.isNotEmpty(userTaxiOrderts)) {
				for (UserTaxiOrdert userTaxiOrdert : userTaxiOrderts) {
					//拷贝属性
					ResOrderListDTO resOrderDTO = new ResOrderListDTO();
					BeanUtils.copyProperties(userTaxiOrdert, resOrderDTO);
					resOrderDTOs.add(resOrderDTO);
				}
			}
			if (CollectionUtils.isNotEmpty(userSpecialOrderts)) {
				for (UserSpecialOrdert userSpecialOrdert : userSpecialOrderts) {
					//拷贝属性
					ResOrderListDTO resOrderDTO = new ResOrderListDTO();
					BeanUtils.copyProperties(userSpecialOrdert, resOrderDTO);
					resOrderDTOs.add(resOrderDTO);
				}
			}
		}

		if (CollectionUtils.isNotEmpty(userOrders)) {
			for (UserOrder userOrder : userOrders) {
				//拷贝属性
				ResOrderListDTO resOrderDTO = new ResOrderListDTO();
				BeanUtils.copyProperties(userOrder, resOrderDTO);
				resOrderDTOs.add(resOrderDTO);
			}
		}
		if (CollectionUtils.isNotEmpty(userSpecialOrders)) {
			for (UserSpecialOrder userSpecialOrder : userSpecialOrders) {
				//拷贝属性
				ResOrderListDTO resOrderDTO = new ResOrderListDTO();
				BeanUtils.copyProperties(userSpecialOrder, resOrderDTO);
				resOrderDTOs.add(resOrderDTO);
			}
		}
		Integer pageNo = (int) (Math.ceil(start / length) + 1);// 当前页
		Integer totalPageNo = (int) (Math.ceil(resOrderDTOs.size() / length));//总共页面
		Page<ResOrderListDTO> pageResults = new Page<ResOrderListDTO>(pageNo, length);
		// 截取符合要求的数据
		SortUtil<ResOrderListDTO> sortUtil = new SortUtil<ResOrderListDTO>();
		sortUtil.sortByMethod(resOrderDTOs, "getCreateTime", true);

		pageResults.setTotalRow(resOrderDTOs.size());
		pageResults.setTotalPage(totalPageNo);

		//分页数据
		List<ResOrderListDTO> results = new ArrayList<ResOrderListDTO>();
		if (pageNo * length > resOrderDTOs.size()) {
			results = resOrderDTOs.subList(start, resOrderDTOs.size());
		} else {
			results = resOrderDTOs.subList(start, pageNo * length);
		}
		// 销毁无用的list
		resOrderDTOs = null;
		
		pageResults.setList(results);
		return pageResults;
	}

	/**
	 * 
	 * @Title: getUserOrderByOrderId 
	 * @Description: 根据订单id查询UserOrder订单
	 * @param orderId  订单id
	 * @return UserOrder    返回类型 
	 * @throws
	 */
	@Override
	public UserOrder getUserOrderByOrderId(String orderId) {
		
		if(orderId == null) {
			logger.warn("getUserOrderByOrderId failed orderId is null.");
			return null;
		}
		return Db.querySqlFirst(UserOrder.class, OrderSql.QUERY_USERORDER_BY_OID, orderId);
	}

	/**
	 * 
	 * @Title: getUserSpecialOrdertByOrderId 
	 * @Description: 根据订单id查询UserSpecialOrdert订单
	 * @param 参数说明 
	 * @return UserSpecialOrdert    返回类型 
	 * @throws
	 */
	@Override
	public UserSpecialOrdert getUserSpecialOrdertByOrderId(String orderId) {
		
		if(orderId == null) {
			logger.warn("getUserSpecialOrdertByOrderId failed orderId is null.");
			return null;
		}
		return Db.querySqlFirst(UserSpecialOrdert.class, OrderSql.QUERY_USERSPECIALORDERT_BY_OID, orderId);
	}

	/**
	 * 
	 * @Title: getUserSpecialOrderByOrderId 
	 * @Description: 根据订单id查询UserSpecialOrder订单
	 * @param orderId
	 * @param  参数说明 
	 * @return UserSpecialOrder    返回类型 
	 * @throws
	 */
	@Override
	public UserSpecialOrder getUserSpecialOrderByOrderId(String orderId) {
		
		if(orderId == null) {
			logger.warn("getUserSpecialOrderByOrderId failed orderId is null.");
			return null;
		}
		return Db.querySqlFirst(UserSpecialOrder.class, OrderSql.QUERY_USERSPECIALORDER_BY_OID, orderId);
	}

	/**
	 * 
	 * @Title: getUserTaxiOrdertByOrderId 
	 * @Description: 根据订单id查询UserTaxiOrdert订单
	 * @param orderId
	 * @param   参数说明 
	 * @return UserTaxiOrdert    返回类型 
	 * @throws
	 */
	@Override
	public UserTaxiOrdert getUserTaxiOrdertByOrderId(String orderId) {
		
		if(orderId == null) {
			logger.warn("getUserTaxiOrdertByOrderId failed orderId is null.");
			return null;
		}
		return Db.querySqlFirst(UserTaxiOrdert.class, OrderSql.QUERY_USERTAXIORDERT_BY_OID, orderId);
	}

	/**
	 * 
	 * @Title: getSpecialOrderCommentByOrderId 
	 * @Description: 根据订单id查询
	 * @param orderId
	 * @param @return  参数说明 
	 * @return SpecialOrderComment    返回类型 
	 * @throws
	 */
	@Override
	public SpecialOrderComment getSpecialOrderCommentByOrderId(String orderId) {
		
		if(orderId == null) {
			logger.warn("getSpecialOrderCommentByOrderId failed orderId is null.");
			return null;
		}
		return Db.querySqlFirst(SpecialOrderComment.class, OrderSql.QUERY_ORDERCOMMENT_BY_OID, orderId);
	}

	/**
	 * 
	 * @Title: getOrderOtherInfoByDriverId 
	 * @Description: 根据司机id查询该笔订单的额外信息
	 * @param driverId
	 * @author hufan
	 * @param @return  参数说明 
	 * @return Map<String,Object>    返回类型 
	 * @throws
	 */
	@Override
	public Map<String, Object> getOrderOtherInfoByDriverId(String driverId) {
		
		if(driverId == null) {
			logger.warn("getOrderOtherInfoByDriverId failed orderId is null.");
			return null;
		}
		return Db.querySqlToMapFirst(OrderSql.DRIVER_BASIC_INFO,driverId);
	}
}
